# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=termcap
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.3.1
pkgrel=5
pkgdesc="Terminal feature database (mingw-w64)"
arch=('any')
url="https://www.gnu.org/software/termutils/"
license=("GPL" "LGPL")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
options=('staticlibs' 'strip')
source=("https://ftp.gnu.org/gnu/termcap/${_realname}-${pkgver}.tar.gz")
sha256sums=('91a0e22e5387ca4467b5bcb18edf1c51b930262fd466d5fda396dd9d26719100')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  autoconf
}

build() {
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir ${srcdir}/build-${MINGW_CHOST} && cd ${srcdir}/build-${MINGW_CHOST}

  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST}

  make
  # Build a shared library.  No need for -fPIC on Windows.
  ${MINGW_PREFIX}/bin/gcc -shared \
    -Wl,--out-implib,libtermcap.dll.a \
    -o libtermcap-0.dll \
    termcap.o tparam.o version.o
}

package() {
  cd  ${srcdir}/build-${MINGW_CHOST}
  make install prefix="${pkgdir}${MINGW_PREFIX}" exec_prefix="${pkgdir}${MINGW_PREFIX}" oldincludedir=
  rm -r "${pkgdir}"${MINGW_PREFIX}/info/
  mkdir -p "${pkgdir}"${MINGW_PREFIX}/{bin,lib}
  install -m 0755 libtermcap-0.dll "${pkgdir}${MINGW_PREFIX}/bin"
  install -m 0644 libtermcap.dll.a "${pkgdir}${MINGW_PREFIX}/lib"

  # pkg-config
  mkdir -p "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig"
  echo -e "prefix=${MINGW_PREFIX}\n\
exec_prefix=\${prefix}\n\
libdir=\${exec_prefix}/lib\n\
includedir=\${prefix}/include\n\
\n\
Name: Termcap\n\
Description: GNU Termcap library and data base that enables programs to use display terminals in a terminal-independent manner\n\
URL: ${url}\n\
Version: ${pkgver}\n\
Libs: -L\${libdir} -ltermcap\n\
Cflags: -I\${includedir}" > "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/termcap.pc"
}
